package com.own.business.creditbank.base;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.own.business.creditbank.function.ResolveFunction;
import com.own.business.creditbank.model.ResultBaseVo;
import com.own.business.creditbank.model.ResultVo;
import com.own.business.creditbank.property.CreditBankProperty;
import com.own.component.common.util.ParamUtil;
import com.own.component.common.util.encrypt.util.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * BaseCreditBankRequestUtil
 *
 * @author chenxueli
 * @date 2022-08-11 11:17:00
 */
@Slf4j
@Component
public class BaseCreditBankRequestUtil {

    @Resource
    private CreditBankProperty creditBankProperty;

    @Resource
    private Md5Util md5Util;

    /**
     * 请求方法
     *
     * @param api             接口名称
     * @param dataMap         请求的数据信息
     * @param resolveFunction 解析方法
     * @param <T>             类型
     * @return 响应数据
     */
    public <T> ResultVo<T> request(String api, Map<String, Object> dataMap, ResolveFunction<T> resolveFunction) {
        var map = requestBody();
        map.putAll(dataMap);
        return request(api, JSON.toJSONString(map, JSONWriter.Feature.NotWriteEmptyArray), resolveFunction);
    }

    /**
     * 请求方法
     *
     * @param api             接口名称
     * @param data            数据
     * @param resolveFunction 解析方法
     * @param <T>             类型
     * @return 响应数据
     */
    public <T> ResultVo<T> request(String api, String data, ResolveFunction<T> resolveFunction) {
        var baseVo = request(api, data);
        // 判断是否是正常的结果
        if (baseVo.getSuccess() == null || !baseVo.getSuccess()) {
            return baseVo.resolve();
        }
        // 返回结果
        return baseVo.resolve(resolveFunction);
    }

    /**
     * 请求方法
     *
     * @param api  请求的api
     * @param data 数据信息
     * @return 响应数据
     */
    public ResultBaseVo request(String api, String data) {
        var body = HttpRequest.post(creditBankProperty.getHost() + api)
                .contentType("application/json")
                .body(data)
                .execute()
                .body();
        // 解析为标准的返回模式
        return JSON.parseObject(body, ResultBaseVo.class);
    }

    /**
     * 获取请求body
     *
     * @return 请求参数对象信息
     */
    public Map<String, Object> requestBody() {
        // 获取时间戳
        var timestamp = System.currentTimeMillis();
        var resultMap = new HashMap<String, Object>();
        // 设置请求的通用体信息
        {
            resultMap.put("appKey", creditBankProperty.getKey());
            resultMap.put("timeStamp", String.valueOf(timestamp));
        }
        // 设置签名数据信息
        {
            var paramMap = new HashMap<String, String>(8);
            paramMap.put("app_key", creditBankProperty.getKey());
            paramMap.put("sys_timestamp", String.valueOf(timestamp));
            var sign = ParamUtil.sort(paramMap) + "&app_secret=" + creditBankProperty.getSecret();
            log.debug("sign:{}", sign);
            var signature = md5Util.encrypt(sign);
            log.debug("signature:{}", signature.toUpperCase());
            resultMap.put("signature", signature.toUpperCase());
        }
        return resultMap;
    }

}
